package com.weight.system.sysmenu.web;

import com.weight.system.sysmenu.PO.SysMenuInfo;
import com.weight.system.sysmenu.VO.SysMenuInfoVO;
import com.weight.system.sysmenu.service.SysMenuInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 菜单信息表
 */
@RestController
@Slf4j
@RequestMapping("/sysMenuInfo")
public class SysMenuInfoController {

	@Autowired
	private SysMenuInfoService sysMenuInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询菜单信息表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForSysMenuInfoList",method=RequestMethod.GET)
	public ModelAndView queryForSysMenuInfoList(){

		Map<Object, Object> map =new HashMap<>();
		map.put("menuStatus", 1); 
		
		//2、查询菜单信息表列表
		List<SysMenuInfo> sysMenuInfoList = sysMenuInfoService.findSysMenuInfoByT(map);
		log.info("sysMenuInfoList={}",sysMenuInfoList);
		List<SysMenuInfoVO> sysMenuInfoVOList = sysMenuInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
		
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("sysmenu/queryForSysMenuInfoList");
		modelAndView.addObject("sysMenuInfoVOList", sysMenuInfoVOList);
		return modelAndView;
	}
	/**
	 * 3.添加、修改菜单信息表操作
	 * @param menuInfoId
	 * @return
	 */
	@RequestMapping(value="/addSysMenuInfo")
	public ModelAndView addSysMenuInfo(@RequestParam(value="menuInfoId",required=false) String menuInfoId){
			//1、创建返回对象
			SysMenuInfoVO sysMenuInfoVO = new SysMenuInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(menuInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("menuInfoId", menuInfoId);
				log.info("修改操作，查询ID：menuInfoId={}",menuInfoId);
				SysMenuInfo sysMenuInfo = sysMenuInfoService.findBySysMenuInfoId(map);
				this.changePoToVO(sysMenuInfoVO,sysMenuInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("sysmenu/addSysMenuInfo");
			modelAndView.addObject("sysMenuInfoVO",sysMenuInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改菜单信息表操作
	 * @param sysMenuInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveSysMenuInfo")
	public JsonResultUtil saveSysMenuInfo(@Valid SysMenuInfoVO sysMenuInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,sysMenuInfoVO);
			
			//2.获取id
			String menuInfoId=sysMenuInfoVO.getMenuInfoId();
			SysMenuInfo sysMenuInfo=new SysMenuInfo();
			BeanUtils.copyProperties(sysMenuInfoVO,sysMenuInfo);
			this.changeVOToPO(sysMenuInfoVO,sysMenuInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(menuInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("menuInfoId", menuInfoId);
				log.info("修改操作，查询ID：menuInfoId={}",menuInfoId);
				//查询是否存在
				sysMenuInfoService.findBySysMenuInfoId(map);
				try{
					sysMenuInfo.setUpdateBy("000");
					sysMenuInfoService.updateSysMenuInfo(sysMenuInfo);
					log.info("更新操作成功：sysMenuInfo={}",sysMenuInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					sysMenuInfo.setMenuInfoId(CommonUtils.getUUId());
					sysMenuInfo.setCreateBy("000");
					sysMenuInfo.setUpdateBy("000");
					sysMenuInfo.setMenuStatus(1);
					sysMenuInfoService.insertSysMenuInfo(sysMenuInfo);
					log.info("添加操作成功：sysMenuInfo={}",sysMenuInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 2.查询菜单信息表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForSysMenuInfoListDG",method=RequestMethod.GET)
	public String queryForSysMenuInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String menuPid = request.getParameter("menuPid");
			String menuName = request.getParameter("menuName");
			String menuUrl = request.getParameter("menuUrl");
			String menuStatus = request.getParameter("menuStatus");
			String menuLevel = request.getParameter("menuLevel");
			String menuDesc = request.getParameter("menuDesc");
			String menuSort = request.getParameter("menuSort");

			Map<Object, Object> map =new HashMap<>();
			map.put("menuPid", menuPid);
			map.put("menuName", menuName);
			map.put("menuUrl", menuUrl);
			map.put("menuStatus", menuStatus);
			map.put("menuLevel", menuLevel);
			map.put("menuDesc", menuDesc);
			map.put("menuSort", menuSort);

			
			//2、查询菜单信息表列表
			List<SysMenuInfo> sysMenuInfoList = sysMenuInfoService.findSysMenuInfoByPage(page,rows,map);
			log.info("sysMenuInfoList={}",sysMenuInfoList);
			List<SysMenuInfoVO> sysMenuInfoVOList = sysMenuInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<SysMenuInfoVO> result = new PageResult<SysMenuInfoVO>(sysMenuInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param sysMenuInfoVO
	 * @param sysMenuInfo
	 * @return
	 */
	public void changeVOToPO(SysMenuInfoVO sysMenuInfoVO,SysMenuInfo sysMenuInfo){
			BeanUtils.copyProperties(sysMenuInfoVO,sysMenuInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param sysMenuInfoVO
	 * @param sysMenuInfo
	 * @return
	 */
	public void changePoToVO(SysMenuInfoVO sysMenuInfoVO,SysMenuInfo sysMenuInfo){
			BeanUtils.copyProperties(sysMenuInfo,sysMenuInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param sysMenuInfo
	 * @return
	 */
	public SysMenuInfoVO changePoToVO(SysMenuInfo sysMenuInfo){
			SysMenuInfoVO sysMenuInfoVO=new SysMenuInfoVO();
			BeanUtils.copyProperties(sysMenuInfo,sysMenuInfoVO);
			

			return sysMenuInfoVO;
	}

	/**
	 * 3.查询菜单信息表--详情页
	 * @param menuInfoId
	 * @return
	 */
	@RequestMapping("/queryForSysMenuInfoDetail")
	public ModelAndView queryForSysMenuInfoDetail(@RequestParam(value="menuInfoId",required=false) String menuInfoId){
			//1.校验id
			CommonUtils.validateExistID(menuInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("menuInfoId", menuInfoId);
			log.info("查询菜单信息表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			SysMenuInfo sysMenuInfo = sysMenuInfoService.findBySysMenuInfoId(map);
			
			SysMenuInfoVO sysMenuInfoVO = changePoToVO(sysMenuInfo);
			//3、返回前台
			log.info("查询菜单信息表查询成功：sysMenuInfo={}",sysMenuInfo);
			modelAndView.setViewName("system/sysmenu/queryForSysMenuInfoDetail");
			modelAndView.addObject("sysMenuInfoVO",sysMenuInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	
	/**
	 * 6.删除菜单信息表操作
	 * @param menuInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteBySysMenuInfoId")
	public JsonResultUtil deleteBySysMenuInfoId(@RequestParam(value="menuInfoId",required=false) String menuInfoId){
			//1.校验id
			CommonUtils.validateExistID(menuInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("menuInfoId", menuInfoId);
			sysMenuInfoService.findBySysMenuInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除菜单信息表入参，map={}",map);
			
			try{
				sysMenuInfoService.upDelBySysMenuInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除菜单信息表操作---批量删除
	 * @param menuInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteBySysMenuInfoIdBatch")
	public JsonResultUtil deleteBySysMenuInfoIdBatch(@RequestParam(value="menuInfoIdList",required=false) List menuInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(menuInfoIdList);
			
			//2.校验数据库
			sysMenuInfoService.findBySysMenuInfoIdList(menuInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("menuInfoIdList", menuInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除菜单信息表操作入参，map={}",map);
			
			try{
				sysMenuInfoService.upDelBySysMenuInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}